package cc.rengu.igas.smps.core.service.trans;

import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.common.constant.SmpsTreeNodeConstant;
import cc.rengu.igas.smps.common.enums.RespCodeEnum;
import cc.rengu.igas.smps.facade.request.TransResultNoticeRequest;
import cc.rengu.igas.smps.facade.response.TransResultNoticeResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 消息推送服务
 * Created by 王成 on 2018/9/5.
 */
public class TransResultNoticeService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {

        try {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            TransResultNoticeRequest transResultNoticeRequest = new TransResultNoticeRequest();
            TransResultNoticeResponse transResultNoticeResponse = new TransResultNoticeResponse();
            ConvertUtil.convertOutput(transResultNoticeRequest);
            rglog.debug("transResultNoticeRequest = <{}>", JSONObject.toJSONString(transResultNoticeRequest));
            if (null == transResultNoticeRequest.getContent()) {
                rglog.error("推送内容:<{}>为空!", transResultNoticeRequest.getPushMsgId(), transResultNoticeRequest.getContent());
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            /* 将推送信息设置到内部节点 */
            xmlTreeUtil.setXmlTreeByteValue(SmpsTreeNodeConstant.SEND_MSG, transResultNoticeRequest.getContent().getBytes());
            /* 将请求中的交易码放进去*/
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, transResultNoticeRequest.getHeader().getTxnNum());
            /* 交易结果推送给商户服务后台 */
            if (!StringUtil.isEmptyOrNull(transResultNoticeRequest.getNoticeUrl()) && transResultNoticeRequest.getNoticeUrl().trim().startsWith("http")) {
                /* 设置通知的URL到内部树 */
                xmlTreeUtil.setXmlTreeStringValue("_MESSAGEHEAD/@client_url", transResultNoticeRequest.getNoticeUrl());
                /* 异步通知给商户 */
                int iReturnCode = schedule.callService(SmpsServiceConstant.MSG_PUSH_SERVICE, 0);
                if (0 != iReturnCode) {
                    rglog.error("交易结果推送给商户后台失败，noticeUrl:<{}>,iReturnCode:<{}>", transResultNoticeRequest.getNoticeUrl(), iReturnCode);
                    transResultNoticeResponse.setRespCode(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                    transResultNoticeResponse.setRespDesc(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                } else {
                    /* 获取推送响应结果 */
                    transResultNoticeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                    transResultNoticeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                    byte[] recvByteMsg = xmlTreeUtil.getXmlTreeByteValue(SmpsTreeNodeConstant.RECV_MSG);
                    if (null != recvByteMsg) {
                        transResultNoticeResponse.setPushResult(new String(recvByteMsg));
                        rglog.info("推送响应结果为:<{}>", transResultNoticeResponse.getPushResult());
                    }
                }
            }/* else if (!StringUtil.isEmptyOrNull(transResultNoticeRequest.getPushMsgId())) {
             *//* 将消息推送标识设置到http头中 *//*
                xmlTreeUtil.setXmlTreeStringValue(SmpsTreeNodeConstant.DEFINE_HTTP_NOTICE_NODE, transResultNoticeRequest.getPushMsgId());
                *//* 设备用户自定义参数 *//*
                xmlTreeUtil.setXmlTreeStringValue(SmpsTreeNodeConstant.DEFINE_HTTP_ARG_LIST, SmpsTreeNodeConstant.DEFAULT_USER_ARG_LIST);
                *//* 消息通知推送到redis服务器 *//*
                int iReturnCode = schedule.callService(SmpsServiceConstant.MSG_PUSH_SERVICE, 0);
                if (0 != iReturnCode) {
                    rglog.error("交易结果推送到Redis失败，pushMsgId:<{}>,iReturnCode:<{}>", transResultNoticeRequest.getPushMsgId(), iReturnCode);
                    transResultNoticeResponse.setRespCode(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                    transResultNoticeResponse.setRespDesc(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                } else {
                    *//* 获取推送响应结果 *//*
                    transResultNoticeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                    transResultNoticeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                    byte[] recvByteMsg = xmlTreeUtil.getXmlTreeByteValue(SmpsTreeNodeConstant.RECV_MSG);
                    if (null != recvByteMsg) {
                        transResultNoticeResponse.setPushResult(new String(recvByteMsg));
                        rglog.info("推送响应结果为:<{}>", transResultNoticeResponse.getPushResult());
                    }
                }
            }*/
            ConvertUtil.convertInput(transResultNoticeResponse);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("消息推送失败:{}", byteArrayOutputStream.toString());
            return -1;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
